﻿package emusic.api.clients 
{
	import emusic.api.events.M3UEvent;
	import emusic.api.feeds.SampleTrackFeed;
	import emusic.ws.AbstractProxyClient;
	
	/**
	 * ...
	 * @author Martin Legris ( http://blog.martinlegris.com )
	 */
	
	 // public static const M3U_RECEIVED:String = "m3u_received_event";
	 
	 /**
	 Dispatched by an InteractiveObject continually while a drag gesture remains within its doundary.
	 @eventType	emusic.api.events.M3UEvent.M3U_RECEIVED
	 */
	[Event(name="m3u_received_event", type="emusic.api.events.M3UEvent")]
	
	public class M3UClient extends AbstractProxyClient
	{
		private static var _instance:M3UClient
		public static function getInstance():M3UClient
		{
			if (_instance == null)
				_instance = new M3UClient();
				
			return _instance;
		}
		
		override protected function doUrlSuccess(result:Object):void
		{
			trace("doM3UReceived");
			var requestId:Number = parseInt(result.requestId);
			var wrapper:Object = getWrapperById(requestId);
			var httpCode:String = result.httpCode;
			var content:String = result.content;

			if (httpCode == "200")
			{
				var feed:SampleTrackFeed = new SampleTrackFeed(content);
				dispatchEvent(new M3UEvent(M3UEvent.M3U_RECEIVED, feed, requestId));
			}
			else
			{
				trace("error:" + httpCode);
			}
		}
	}
}